package com.sjy.project.fym.place.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.service.IFyPlaceService;
import com.sjy.project.system.dept.domain.Dept;
import com.sjy.project.system.user.domain.User;
import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.framework.web.domain.Ztree;
import com.sjy.common.exception.BusinessException;
import com.sjy.common.utils.GPSUtil;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 防疫点信息Controller
 * 
 * @author sjy
 * @date 2022-04-17
 */
@Controller
@RequestMapping("/fym/place")
public class FyPlaceController extends BaseController
{
    private String prefix = "fym/place";
    
    private final static Map<Long, FyPlace> places = new LinkedHashMap<Long, FyPlace>();
    
    @Autowired
    private IFyPlaceService fyPlaceService;

    @RequiresPermissions("fym:place:view")
    @GetMapping()
    public String place()
    {
        return prefix + "/place";
    }

    /**
     * 查询防疫点信息列表
     */
    @RequiresPermissions("fym:place:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FyPlace fyPlace)
    {
        startPage();
        List<FyPlace> list = fyPlaceService.selectFyPlaceList(fyPlace);
        return getDataTable(list);
    }

    /**
     * 导入数据
     */
    @RequiresPermissions("fym:place:import")
    @Log(title = "防疫点信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        List<FyPlace> list = util.importExcel(file.getInputStream());
        String message = fyPlaceService.importPlace(list, updateSupport);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("fym:place:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        return util.importTemplateExcel("防疫点数据");
    }
    /**
     * 导出防疫点信息列表
     */
    @RequiresPermissions("fym:place:export")
    @Log(title = "防疫点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FyPlace fyPlace)
    {
        List<FyPlace> list = fyPlaceService.selectFyPlaceList(fyPlace);
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        return util.exportExcel(list, "防疫点信息数据");
    }

    /**
     * 新增防疫点信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    
    /**
     * 展示二维码
     */
    @GetMapping("/detail/{placeId}")
    public String detail(@PathVariable("placeId") Long placeId, ModelMap mmap,HttpServletRequest request)
    {
    	FyPlace fyPlace = fyPlaceService.selectFyPlaceByPlaceId(placeId);
        mmap.put("ctx", request.getContextPath());
    	mmap.put("fyPlace", fyPlace);
        return prefix + "/print";
    }
    

    /**
     * 新增保存防疫点信息
     */
    @RequiresPermissions("fym:place:add")
    @Log(title = "防疫点信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FyPlace fyPlace)
    {
    	//更新lngSjy latSjy 到 lng lat
        if(fyPlace.getLatSjy() != null && fyPlace.getLatSjy() != null) {
        	//double[] lnglat = GPSUtil.bd09_To_gps84(new Double(fyPlace.getLatSjy()+""), new Double(fyPlace.getLngSjy()+""));
            fyPlace.setLng(fyPlace.getLngSjy());
            fyPlace.setLat(fyPlace.getLatSjy());
        }
        
        return toAjax(fyPlaceService.insertFyPlace(fyPlace));
    }

    /**
     * 修改防疫点信息
     */
    @RequiresPermissions("fym:place:edit")
    @GetMapping("/edit/{placeId}")
    public String edit(@PathVariable("placeId") Long placeId, ModelMap mmap)
    {
        FyPlace fyPlace = fyPlaceService.selectFyPlaceByPlaceId(placeId);
        mmap.put("fyPlace", fyPlace);
        return prefix + "/edit";
    }
    /**
     * 修改保存防疫点信息
     */
    @RequiresPermissions("fym:place:edit")
    @Log(title = "防疫点信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FyPlace fyPlace)
    {
    	//更新lngSjy latSjy 到 lng lat
//        double[] lnglat = GPSUtil.bd09_To_gps84(new Double(fyPlace.getLatSjy()+""), new Double(fyPlace.getLngSjy()+""));
//        fyPlace.setLng(new BigDecimal(lnglat[1]));
//        fyPlace.setLat(new BigDecimal(lnglat[0]));
        if(fyPlace.getLatSjy() != null && fyPlace.getLatSjy() != null) {
        	//double[] lnglat = GPSUtil.bd09_To_gps84(new Double(fyPlace.getLatSjy()+""), new Double(fyPlace.getLngSjy()+""));
            fyPlace.setLng(fyPlace.getLngSjy());
            fyPlace.setLat(fyPlace.getLatSjy());
        }
        
        return toAjax(fyPlaceService.updateFyPlace(fyPlace));
    }
    /**
     * 批量修改防疫点信息
     */
    @RequiresPermissions("fym:place:edit")
    @GetMapping("/editMultiple/{placeIds}")
    public String editMultiple(@PathVariable("placeIds") String placeIds, ModelMap mmap)
    {
    	System.out.println(placeIds);
        //FyPlace fyPlace = null;//fyPlaceService.selectFyPlaceByPlaceId(placeId);
        //批量获取场所的名称
        String placeNames = fyPlaceService.selectFyPlaceNamesByPlaceIds(placeIds);
    	mmap.put("placeIds", placeIds);
        mmap.put("placeNames", placeNames);
        return prefix + "/edit_mulitiple";
    }
    /**
     * 批量修改保存防疫点信息
     */
    @RequiresPermissions("fym:place:edit")
    @Log(title = "防疫点信息批量", businessType = BusinessType.UPDATE)
    @PostMapping("/editMultiple")
    @ResponseBody
    public AjaxResult editMultipleSave(String placeIds,String status,Integer signInRadius)
    {
    	System.out.println(placeIds+"---"+status+"---"+signInRadius);
        return toAjax(fyPlaceService.updateFyPlaceMultiple(placeIds,status,signInRadius));
    }
    /**
     * 批量打印防疫点信息
     */
    @GetMapping("/printMultiple/{placeIds}")
    public String printMultiple(@PathVariable("placeIds") String placeIds, ModelMap mmap,HttpServletRequest request)
    {
    	Map<String,Object> params = new HashMap<String,Object>();
    	params.put("placeIds", placeIds);
    	
    	FyPlace fyPlace = new FyPlace();
    	fyPlace.setParams(params);
    	
        List<FyPlace> fyPlaces = fyPlaceService.selectFyPlaceList(fyPlace);

        mmap.put("ctx", request.getContextPath());
    	mmap.put("fyPlaces", fyPlaces);
        return prefix + "/print_mulitiple";
    }

    /**
     * 加载场所列表树
     */
    @PostMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = fyPlaceService.selectPlaceTree(new FyPlace());
        return ztrees;
    }
    
    /**
     * 删除防疫点信息
     */
    @RequiresPermissions("fym:place:remove")
    @Log(title = "防疫点信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fyPlaceService.deleteFyPlaceByPlaceIds(ids));
    }
    
}
